package com.springcloud.alibabanacosconfig.mq.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;

/**
 * @version V1.0
 * @Author 宋聚智
 * @Date 2019/10/23 14:16
 * @description
 * @Modify By：
 * @ModifyTime： 2019/10/23
 * @Modify marker：
 **/
@Service
@Slf4j
public class InitService {
    public static final int ThreadNum=500;
    private static int mobile=0;
    //@
    @Autowired
    CommonMqService commonMqService;
    public void generateMultiThread(){
        log.info("开始初始化线程数");
        try{
            CountDownLatch countDownLatch = new CountDownLatch(1);
            for(int i=0;i<ThreadNum;i++){
                new Thread(new RunThread(countDownLatch)).start();
            }
            countDownLatch.countDown();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private class RunThread implements  Runnable{
        private final CountDownLatch startLatch;
        public  RunThread(CountDownLatch startLatch){
            this.startLatch=startLatch;
        }
        public void run(){
            try {
                startLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mobile+=1;
            commonMqService.sendRobbingMsgV2(mobile+"");

        }
    }
}
